import {
  ADD_FILE_OR_FOLDER,
  OPENED_FILEIDS_ADD,
  OPENED_FILEIDS_DELETE,
  UPDATE_ACTIVE_FILE_ID,
  UPDATE_FILE_OR_FOLDERNAME,
  REMOVE_FILE_OR_FOLDER,
  UPDATE_CLIP_FILE_OR_FOLDER,
  PASTE_FILE_OR_FOLDER,
  UPDATE_COPY_ID,
} from "../constant";
import {
  deleteChildren,
  doPaste,
  clipFileOrFolder,
} from "../../utils/fileUtils";

const storageData = localStorage.getItem("state");

//文件信息reducer
const initState = JSON.parse(storageData) || {
  //对象数组将唯一值提取出来作为键，剩下信息作为值。转为对象，方便增删改查
  //文件s对象
  filesObj: {},
  //文件夹s对象，同样也是对象数组转对象，方便增删改查
  foldersObj: {
    //根文件夹的id为0
    0: {
      //文件夹名称
      name: "root-folder",
      //当前文件夹的父级文件夹,根文件夹父级id为null
      pid: "",
    },
  },
  //编辑器打开的文件id集合
  openedFilesIdArr: [],
  //编辑器当前正在编辑的文件id
  activeFileId: "",

  //复制的文件(夹)id，为空代表当前没有进行复制操作
  copyId: "",

  //剪切板中文件（夹）的id，空串代表剪切板中没有文件(夹)
  clipFileId: "",
  //被剪切的文件（夹）的数据
  clipedFileObj: {},
};

export default function filesReducer(preState = initState, action) {
  const { type, payload } = action;
  const { filesObj, foldersObj } = preState;
  if (type === ADD_FILE_OR_FOLDER) {
    const { id, bFile, name, content, pid } = payload;
    if (bFile) {
      //文件新增
      return {
        ...preState,
        filesObj: {
          ...preState.filesObj,
          [id]: {
            name,
            content,
            pid,
          },
        },
      };
    } else {
      //文件夹新增
      return {
        ...preState,
        foldersObj: {
          ...preState.foldersObj,
          [id]: {
            name,
            pid,
          },
        },
      };
    }
  } else if (type === REMOVE_FILE_OR_FOLDER) {
    if (Object.keys(filesObj).includes(payload)) {
      //删除文件
      const newFilesObj = { ...preState.filesObj };
      //移除这一项，使用delete删除
      delete newFilesObj[payload];
      return {
        ...preState,
        filesObj: newFilesObj,
      };
    } else {
      //删除文件夹
      return deleteChildren(payload, preState);
    }
  } else if (type === OPENED_FILEIDS_ADD) {
    const newArr = [...preState.openedFilesIdArr];
    newArr.push(payload);
    return { ...preState, openedFilesIdArr: newArr };
  } else if (type === OPENED_FILEIDS_DELETE) {
    const newArrD = preState.openedFilesIdArr.filter(
      (item) => item !== payload
    );
    return { ...preState, openedFilesIdArr: newArrD };
  } else if (type === UPDATE_ACTIVE_FILE_ID) {
    return { ...preState, activeFileId: payload };
  } else if (type === UPDATE_FILE_OR_FOLDERNAME) {
    console.log(payload);
    if (payload.type.indexOf("file") >= 0) {
      return {
        ...preState,
        filesObj: {
          ...filesObj,
          [payload.id]: { ...filesObj[payload.id], name: payload.name },
        },
      };
    } else {
      return {
        ...preState,
        foldersObj: {
          ...foldersObj,
          [payload.id]: { ...foldersObj[payload.id], name: payload.name },
        },
      };
    }
  } else if (type === UPDATE_CLIP_FILE_OR_FOLDER) {
    return clipFileOrFolder(payload, filesObj, foldersObj, preState);
  } else if (type === PASTE_FILE_OR_FOLDER) {
    //粘贴文件或者文件夹
    const pid = payload;
    return doPaste(pid, preState);
  } else if (type === UPDATE_COPY_ID) {
    return {
      ...preState,
      clipFileId: "",
      clipedFileObj: {},
      copyId: payload,
    };
  } else {
    return preState;
  }
}
